//
//  LZFileLoader.m
//  Project
//
//  Created by liting on 2025/8/29.
//

#import "LZFileLoader.h"

@implementation LZFileLoader

#pragma mark - 单例
+ (instancetype)sharedLoader {
    static LZFileLoader *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

#pragma mark - 从Main Bundle加载JSON
- (id)loadJSONFromMainBundleWithFileName:(NSString *)fileName fileType:(NSString *)fileType {
    return [self loadJSONFromMainBundleWithFileName:fileName fileType:fileType error:nil];
}

- (id)loadJSONFromMainBundleWithFileName:(NSString *)fileName
                               fileType:(NSString *)fileType
                                  error:(NSError **)error {

    // 默认文件类型为json
    NSString *type = fileType ?: @"json";

    // 获取文件路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:type];
    if (!filePath) {
        if (error) {
            *error = [NSError errorWithDomain:@"LZFileLoaderErrorDomain"
                                         code:1001
                                     userInfo:@{NSLocalizedDescriptionKey: @"文件不存在"}];
        }
        return nil;
    }

    // 读取文件数据
    NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:0 error:error];
    if (!jsonData) {
        return nil;
    }

    // 解析JSON
    return [NSJSONSerialization JSONObjectWithData:jsonData
                                          options:kNilOptions
                                            error:error];
}

#pragma mark - 从Documents目录加载JSON
- (id)loadJSONFromDocumentsWithFileName:(NSString *)fileName {
    NSString *documentsPath = [self getDocumentsDirectoryPath];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];

    if (![self fileExistsAtPath:filePath]) {
        NSLog(@"文件不存在: %@", filePath);
        return nil;
    }

    NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
    if (!jsonData) {
        return nil;
    }

    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                   options:kNilOptions
                                                     error:&error];

    if (error) {
        NSLog(@"JSON解析错误: %@", error.localizedDescription);
        return nil;
    }

    return jsonObject;
}

#pragma mark - 从特定Bundle加载
- (id)loadJSONFromBundle:(NSString *)bundleName withFileName:(NSString *)fileName {
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:bundleName ofType:@"bundle"];
    if (!bundlePath) {
        NSLog(@"Bundle不存在: %@", bundleName);
        return nil;
    }

    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *filePath = [bundle pathForResource:fileName ofType:@"json"];

    if (!filePath) {
        NSLog(@"文件在Bundle中不存在: %@/%@", bundleName, fileName);
        return nil;
    }

    NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
    if (!jsonData) {
        return nil;
    }

    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                   options:kNilOptions
                                                     error:&error];

    if (error) {
        NSLog(@"JSON解析错误: %@", error.localizedDescription);
        return nil;
    }

    return jsonObject;
}

#pragma mark - 文件路径相关
- (NSString *)getFilePathInMainBundle:(NSString *)fileName fileType:(NSString *)fileType {
    return [[NSBundle mainBundle] pathForResource:fileName ofType:fileType];
}

- (NSString *)getDocumentsDirectoryPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths firstObject];
}

- (BOOL)fileExistsAtPath:(NSString *)filePath {
    return [[NSFileManager defaultManager] fileExistsAtPath:filePath];
}

@end
